package com.berry.MedCheck;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

/**
 * Created with IntelliJ IDEA.
 * User: Airay.Rong
 * Date: 13-12-4
 * Time: 下午2:42
 * To change this template use File | Settings | File Templates.
 */
public class BaseActivity extends Activity {
	public ImageButton mShowMenu =null;
	public ImageButton mMenuData =null;
	public ImageButton mMenuGraph =null;
	public ImageButton mMenuSetting =null;

	public MenuCallback mListener;
	public interface MenuCallback {
		void onMenuClick(int id);
	}
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	@Override
	protected void onResume() {
		super.onResume();
		LinearLayout partMenu = (LinearLayout) findViewById(R.id.part_menu);
		if (partMenu != null) {
			partMenu.setVisibility(View.GONE);
		}
		LinearLayout partMain = (LinearLayout) findViewById(R.id.part_main);
		if (partMain != null) {
			partMain.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_bg));
		}


		mShowMenu = (ImageButton) findViewById(R.id.show_menu);
		if (mShowMenu != null){
			mShowMenu.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					showMenu();
//					Intent mi = new Intent();
//					mi.setClass(getApplicationContext(), MenuActivity.class);
//					startActivity(mi);
//					finish();
				}
			});
		}
		mMenuData = (ImageButton) findViewById(R.id.menu_spo2);
		if (mMenuData != null) {
			mMenuData.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivity(new Intent().setClass(BaseActivity.this,DataActivity.class));
					if (mListener != null) {
						mListener.onMenuClick(v.getId());
					}
				}
			});
		}
		mMenuGraph = (ImageButton) findViewById(R.id.menu_graph);
		if (mMenuGraph != null) {
			mMenuGraph.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivity(new Intent().setClass(BaseActivity.this, GraphActivity.class));
					if (mListener != null) {
						mListener.onMenuClick(v.getId());
					}
				}
			});
		}
		mMenuSetting = (ImageButton) findViewById(R.id.menu_setting);
		if (mMenuSetting != null) {
			mMenuSetting.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivity(new Intent().setClass(BaseActivity.this, SettingActivity.class));
					if (mListener != null) {
						mListener.onMenuClick(v.getId());
					}

				}
			});
		}

	}

	private void showMenu() {
		LinearLayout partMenu = (LinearLayout) findViewById(R.id.part_menu);
		LinearLayout partMain = (LinearLayout) findViewById(R.id.part_main);
		if (partMenu.getVisibility() == View.GONE) {
			partMenu.setVisibility(View.VISIBLE);
			if (partMain != null) {
				partMain.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_bg_shandow));
			}
		} else {
			partMenu.setVisibility(View.GONE);
			if (partMain != null) {
				partMain.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_bg));
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			if (mShowMenu != null){
				mShowMenu.performClick() ;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	public void   showMenuButton(boolean bShow) {
		if (mShowMenu == null) {
			return;
		}
		if (bShow == false) {
			mShowMenu.setVisibility(View.GONE);
		} else {
			mShowMenu.setVisibility(View.VISIBLE);
		}
	}
}